package com.lxb.stock;

import java.util.Date;
import java.util.StringTokenizer;

public class SStockInfo 
{
	private final static int STOCK_NAME = 0;
	private final static int STOCK_TOPEN = 1;
	private final static int STOCK_YCLOSE = 2;
	private final static int STOCK_CUR = 3;
	private final static int STOCK_THIGH = 4;
	private final static int STOCK_TLOW = 5;
	private final static int STOCK_BUY = 6;
	private final static int STOCK_SELL = 7;
	private final static int STOCK_VOL = 8;
	private final static int STOCK_AMO = 9;
	private final static int STOCK_BUY1VOL = 10;
	private final static int STOCK_BUY1 = 11;
	private final static int STOCK_BUY2VOL = 12;
	private final static int STOCK_BUY2 = 13;
	private final static int STOCK_BUY3VOL = 14;
	private final static int STOCK_BUY3 = 15;
	private final static int STOCK_BUY4VOL = 16;
	private final static int STOCK_BUY4 = 17;
	private final static int STOCK_BUY5VOL = 18;
	private final static int STOCK_BUY5 = 19;
	private final static int STOCK_SELL1VOL = 20;
	private final static int STOCK_SELL1 = 21;
	private final static int STOCK_SELL2VOL = 22;
	private final static int STOCK_SELL2 = 23;
	private final static int STOCK_SELL3VOL = 24;
	private final static int STOCK_SELL3 = 25;
	private final static int STOCK_SELL4VOL = 26;
	private final static int STOCK_SELL4 = 27;
	private final static int STOCK_SELL5VOL = 28;
	private final static int STOCK_SELL5 = 29;
	private final static int STOCK_DATE = 30;
	private final static int STOCK_TIME = 31;
	
	String[] infos;
	String id;
	
	public SStockInfo(String rawdata, String id)
	{
		infos = new String[STOCK_TIME + 1];
		this.id = id;
		parseRawData(rawdata);
	}
	
	public float getCurPrice()
	{
		if (infos[STOCK_CUR] == null)
			return 0;
		return Float.parseFloat(infos[STOCK_CUR]);
	}
	
	public int getVolume()
	{
		if (infos[STOCK_VOL] == null)
			return 0;
		return (Integer.parseInt(infos[STOCK_VOL]) / 100);
	}
	
	public int getAmount()
	{
		if (infos[STOCK_AMO] == null)
			return 0;
		return (Integer.parseInt(infos[STOCK_AMO]) / 10000);
	}
	
	public float getAveragePrice()
	{
		if ((infos[STOCK_VOL] == null)
			|| (infos[STOCK_AMO] == null))
			return 0;
		float volume = Integer.parseInt(infos[STOCK_VOL]);
		float amount = Integer.parseInt(infos[STOCK_AMO]);
		if (volume == 0)
			return 0;
		return (float)(amount / volume); 
	}
	
	public float getTodayOpen()
	{
		if (infos[STOCK_TOPEN] == null)
			return 0;
		return Float.parseFloat(infos[STOCK_TOPEN]);
	}
	
	public float getYesterdayClose()
	{
		if (infos[STOCK_YCLOSE] == null)
			return 0;
		return Float.parseFloat(infos[STOCK_YCLOSE]);
	}
	
	public float getTodayHigh()
	{
		if (infos[STOCK_THIGH] == null)
			return 0;
		return Float.parseFloat(infos[STOCK_THIGH]);
	}

	public float getTodayLow()
	{
		if (infos[STOCK_TLOW] == null)
			return 0;
		return Float.parseFloat(infos[STOCK_TLOW]);
	}
	
	public float getPercentage()
	{
		float yclose = getYesterdayClose();
		float delta = getCurPrice() - yclose;
		if (yclose == 0)
			return 0;
		float percent = (float)((delta / yclose) * 100);
		return (float)(Math.round(percent * 100)) / 100;
	}
	
	public String getName()
	{
		if (infos[STOCK_NAME] == null)
			return new String("");
		return infos[STOCK_NAME];
	}
	
	public String getTime()
	{
		if (infos[STOCK_TIME] == null)
			return new String("");
		return infos[STOCK_TIME];
	}

	private void parseRawData(String rawData)
	{
		int firstQuot = rawData.indexOf("\"");
		if (firstQuot == -1)
			return;
		int lastQuot = rawData.indexOf("\"", firstQuot + 1);
		if (lastQuot == -1)
			return;
		StringTokenizer st = new StringTokenizer(rawData.substring(firstQuot + 1, lastQuot), ",");
		int count = 0;
		while(st.hasMoreTokens())
		{
			infos[count] = st.nextToken();
			count ++;
		}
	}

}
